import os
import requests
import json
import re
import urllib3

urllib3.disable_warnings()
host = 'https://dm-81.data.aliyun.com/rest/160601/ip/getIpInfo.json?ip='
appcode = '079f5054a1064a0c9811b2ac875c7a84'
headers={}
headers['Authorization']='APPCODE ' + appcode

host_name='stihl.service-now.com'

def get_ip_address(ip_address):
    res = requests.get(url=host + ip_address, headers=headers, verify=False).json()
    address=res['data']['country'],res['data']['city'],res['data']['isp']
    return address

tracert_list=os.popen('TRACERT {host_name}'.format(host_name=host_name)).read().splitlines()

def match_ip(str):
    reip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
    re_match_ip=reip.match(str)
    if  re_match_ip== None:
        return 'None'
    else:
        ip = re_match_ip.group()
        return ip

def check_delay():
    element_count=complete_ip_list.count("*")
    if element_count==1:
        delay=complete_ip_list[1:6]
    elif element_count==2:
        delay=complete_ip_list[1:5]
    elif element_count==3:
        delay=complete_ip_list[1:4]
    else:
        delay=complete_ip_list[1:7]
    return str(delay)

ip_address_list=tracert_list[4:]
for complete_ip in ip_address_list:
    complete_ip_list=complete_ip.split( )
    for ip_str in complete_ip_list:
        if match_ip(ip_str) != 'None':
            ip_number = match_ip(ip_str)
            dalay=re.sub(r'[\,\']','',check_delay())
            print(ip_number, dalay, get_ip_address(ip_number))
        elif len(complete_ip_list) == 1:
            print(complete_ip_list[0])
            exit()